/*
 * Copyright (c) 2010 Network Security Lab (NSL), Department of Electrical
 *                    Engineering, University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: He Wu mdzz@uw.edu
 */

/**
 * @file network.h
 * @brief Network related functions (send/receive/ACK).
 *
 * LITTLE ENDIAN! LSB goes in lower index.
 */

#ifndef NETWORK_H
#define NETWORK_H

#include <sys/types.h>
#include "tosmac.h"

#define MAX_PAYLOAD_SIZE 28	//!< max size for data payload
#define DATA_SIZE 26        //!< data payload size (MAX_PAYLOAD_SIZE - 2)
#define CHANNEL 26          //!< channel used for radio (default 26)
#define POWER_LEVEL 31      //!< radio power level (3 to 31)
// ACK related
#define RETRIES 3           //!< # of retires if packet fails to send
#define ACK_TIMEOUT 2       //!< time for the sender to wait for ACK (in sec)
#define RECV_TIMEOUT 1      //!< time for the receiver to wait for a packet from sender (in sec)
#define RECV_TIMEOUT_U 20000  //!< time for the receiver to wait for a packet from sender (in us)
#define WAIT_TIME 3000      //!< wait time between sending packets (in us)
#define ACK_RESEND 1        //!< # of ACK resend (default 1)
// packet type definition
#define DATA_PKT_TYPE     1 //!< data packet NOT USED
#define ACK_PKT_TYPE      2 //!< ACK packet
#define SENSOR_PKT_TYPE   3 //!< sensor packet
#define HEALTH_DATA_TYPE  4 //!< health data reported from node to base
#define CMD_PKT_TYPE      7 //!< command packet type (default 7)
#define ANCHOR_BROADCAST  8 //!< anchor node broadcast packet type (default 8)
#define ROUTING_PKT_TYPE  9 //!< back to base routing packet type (default 9)
#define TO_NODE_ROUTING_PKT_TYPE  10  //!< to each node routing packet type (default 10)
#define TO_BASE_DATA_PKT_TYPE     11  //!< to base localization data packet type (default 11)
// MISC
#define CMD_PKT_SIZE 12       //!< command packet size (12 bytes)
#define MIN_RSSI -100         //!< value below sensitivity of CC2420
#define BROADCAST_ADDR 0xffff //!< broadcast address
// error message
#define RADIO_DRIVER_ERROR  1
#define SEND_ERROR          2
#define RECV_TIMEOUT_ERROR  3
#define RECV_ERROR          4
#define ACK_ERROR           5
#define ACK_TIMEOUT_ERROR   6

// debug switch, if true will print out received packet info
#define network_debug 0

/******************************************************************************/

/**
 * Incoming packet struct
 */
typedef struct packet_st
{
  __u8 data[DATA_SIZE]; //!< data payload
  __u16 senderAddr;     //!< sender address
  __u16 type;           //!< packet type (port number)
  __s8 rss;             //!< RSSI
  __u8 seq;             //!< sequence number
} packet;

/******************************************************************************/

/**
 * @brief Initialize imote2 radio (cc2420).
 * @param pwr_level Power level of cc2420, 3 - 31.
 * @param addr Address of current node.
 * @return 0 if all is good.
 */
int cc2420_init(int pwr_level, __u16 addr);

/**
 * @brief Initialize packet.
 * @param pkt Pointer to packet.
 * @param type Type of packet
 */
void initPkt(packet *pkt, __u16 type);

/**
 * @brief Send packet.
 * @param destAddr Destination address.
 * @param out_pkt Pointer to packet to be sent.
 * @param ACK_en Set to enable ACK.
 * @return 0 if all is good
 *
 * This function holds the radio lock
 */
int h_send(__u16 destAddr, packet *out_pkt, int ACK_en);

/**
 * @brief Receive packet.
 * @param localAddr Local address.
 * @param in_pkt Pointer to location to store the incoming packet.
 * @return 0 if all is good.
 *
 * This function holds the radio mutex lock.
 */
int h_recv(__u16 localAddr, packet *in_pkt);

#endif
